home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / ssmn012c.lha / Sysmon / src / Timer.c < prev    next >
C/C++ Source or Header  |  1995-11-19  |  5KB  |  174 lines

  1. /*
  2. **    $RCSfile: Timer.c,v $
  3. **    $Filename: Timer.c $
  4. **    $Revision: 0.1 $
  5. **    $Date: 1995/11/18 14:14:05 $
  6. **
  7. **    sysmon command timer (version 0.2)
  8. **
  9. **    (C) Copyright 1995 by Etienne Vogt
  10. */
  11.  
  12. #include <dos/rdargs.h>
  13. #include <dos/dostags.h>
  14. #include <dos/dosextens.h>
  15. #include <workbench/startup.h>
  16. #include <devices/timer.h>
  17. #define __USE_SYSBASE
  18. #include <proto/exec.h>
  19. #include <proto/dos.h>
  20. #include <proto/timer.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include "sysmon.h"
  25. #include "sysmon_protos.h"
  26. #include "sysmon_pragmas.h"
  27.  
  28. struct Library *SysmonBase;
  29. struct Library *TimerBase;
  30. static struct RDArgs *myrda;
  31. static struct WBStartup *wbmsg;
  32. static struct timerequest mytimereq;
  33.  
  34. int main(void);
  35. static void cleanexit(int rc);
  36. static struct EClockVal subEClockVal(struct EClockVal e2, struct EClockVal e1);
  37. static char *uptimestr(struct EClockVal *clockval, ULONG clockrate);
  38. static char *cputimestr(struct EClockVal *clockval, ULONG clockrate);
  39. static char *cpuloadstr(struct EClockVal *cpu, struct EClockVal *time);
  40. void __asm __saveds comExit(register __d0 int rc, register __d1 struct EClockVal *data);
  41.  
  42. static UBYTE template[] = "COMMAND/F/A";
  43. static UBYTE version[] = "$VER: Timer 0.2 (19.11.95)";
  44.  
  45. #define OPT_COMMAND    0
  46. #define OPTMAX        1
  47.  
  48.  
  49. int main(void)
  50. { LONG opts[OPTMAX];
  51.   struct CommandLineInterface *mycli;
  52.  
  53.   SysmonBase = NULL;
  54.   TimerBase = NULL;
  55.   wbmsg = NULL;
  56.   myrda = NULL;
  57.  
  58.   if (DOSBase->dl_lib.lib_Version < 37)
  59.   { Printf("Requires AmigaOS V37 or higher.\n");
  60.     exit(30);
  61.   }
  62.  
  63.   if (!(mycli = Cli()))
  64.     cleanexit(20);        /* If started from WB, exit cleanly */
  65.   else
  66.   { struct EClockVal StartTime, EndTime, ElapsedTime;
  67.     static struct EClockVal ElapsedCPU;
  68.     ULONG EClockRate;
  69.  
  70.     memset((char *)opts, 0, sizeof(opts));
  71.     if ((myrda = ReadArgs(template, opts, NULL)) == NULL)
  72.     { PrintFault(IoErr(),NULL);
  73.       cleanexit(20);
  74.     }
  75.  
  76.     if ((SysmonBase = OpenLibrary("sysmon.library",0)) == NULL)
  77.     { PutStr("timer : Couldn't open sysmon.library\n");
  78.       cleanexit(20);
  79.     }
  80.  
  81.     if (OpenDevice(TIMERNAME, UNIT_ECLOCK, (struct IORequest *)&mytimereq, 0L))
  82.       cleanexit(100);
  83.     TimerBase = (struct Library *)mytimereq.tr_node.io_Device;
  84.  
  85.     EClockRate = ReadEClock(&StartTime);
  86.     
  87.     if (SystemTags((STRPTR)opts[OPT_COMMAND],
  88.         SYS_UserShell,    TRUE,
  89.         NP_StackSize,    mycli->cli_DefaultStack << 2,
  90.         NP_ExitCode,    comExit,
  91.         NP_ExitData,    &ElapsedCPU,
  92.         TAG_DONE ))
  93.     { PrintFault(IoErr(), "Error starting command");
  94.       cleanexit(10);
  95.     }
  96.  
  97.     ReadEClock(&EndTime);
  98.     ElapsedTime = subEClockVal(EndTime, StartTime);
  99.  
  100.     Printf("Elapsed Time : %s , CPU Time : %s , CPU Load : %s\n",
  101.     uptimestr(&ElapsedTime, EClockRate),
  102.     cputimestr(&ElapsedCPU, EClockRate),
  103.     cpuloadstr(&ElapsedCPU, &ElapsedTime));
  104.   }
  105.   cleanexit(0);
  106. }
  107.  
  108. static void cleanexit(int rc)
  109. {
  110.   if (SysmonBase) CloseLibrary(SysmonBase);
  111.   if (TimerBase) CloseDevice((struct IORequest *)&mytimereq);
  112.   if (myrda) FreeArgs(myrda);
  113.   exit(rc);
  114. }
  115.  
  116. static struct EClockVal subEClockVal(struct EClockVal e2, struct EClockVal e1)
  117. { BOOL carry = (e1.ev_lo > e2.ev_lo);
  118.  
  119.   e2.ev_lo -= e1.ev_lo;
  120.   e2.ev_hi -= e1.ev_hi;
  121.   if (carry) e2.ev_hi -= 1;
  122.   return e2;
  123. }
  124.  
  125. static char *uptimestr(struct EClockVal *clockval, ULONG clockrate)
  126. { static char buffer[16];
  127.   ULONG seconds;
  128.   UWORD updays, uphours, upminutes, upseconds;
  129.  
  130.   seconds = (ULONG)((clockval->ev_hi * 4294967296.0 + clockval->ev_lo) / (double)clockrate);
  131.   updays = seconds / (24*60*60);
  132.   seconds %= 24*60*60;
  133.   uphours = seconds / (60*60);
  134.   seconds %= 60*60;
  135.   upminutes = seconds / 60;
  136.   upseconds = seconds % 60;
  137.   sprintf(buffer, "%2lu %02lu:%02lu:%02lu", updays, uphours, upminutes, upseconds);
  138.   return buffer; 
  139. }
  140.  
  141. static char *cputimestr(struct EClockVal *clockval, ULONG clockrate)
  142. { static char buffer[18];
  143.   ULONG millis;
  144.   UWORD cpudays, cpuhours, cpuminutes, cpuseconds, cpumillis;
  145.  
  146.   millis = (ULONG)((clockval->ev_hi * 4294967296.0 + clockval->ev_lo) / (double)(clockrate / 1000));
  147.   cpudays = millis / (24*60*60*1000);
  148.   millis %= 24*60*60*1000;
  149.   cpuhours = millis / (60*60*1000);
  150.   millis %= 60*60*1000;
  151.   cpuminutes = millis / (60*1000);
  152.   millis %= 60*1000;
  153.   cpuseconds = millis / 1000;
  154.   cpumillis = millis % 1000;
  155.   sprintf(buffer, "%2lu %02lu:%02lu:%02lu.%03lu", cpudays, cpuhours, cpuminutes, cpuseconds, cpumillis);
  156.   return buffer;
  157. }
  158.  
  159. static char *cpuloadstr(struct EClockVal *cpu, struct EClockVal *time)
  160. { static char buffer[10];
  161.   double cpuload;
  162.  
  163.   cpuload = 100.0 * (cpu->ev_hi * 4294967296.0 + cpu->ev_lo) / (time->ev_hi * 4294967296.0 + time->ev_lo);
  164.   sprintf(buffer, "%5.1f %%", cpuload);
  165.   return buffer;
  166. }
  167.  
  168. void __asm __saveds comExit(register __d0 int rc, register __d1 struct EClockVal *data)
  169. { struct TaskInfo *mytinfo;
  170.  
  171.   mytinfo = smFindTaskInfo(NULL);
  172.   *data = mytinfo->ti_CPUTime;
  173. }
  174.